import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/*
* 根据 imageUrl 获取的图片文件
* 提交到uploadUrl接口的文件参数里面  提交文件成功
*
* */

public class UploadImage {
    public static void main(String[] args) {
        /*
        *
        * boundary  form-data的分割表头
        * */
        String imageUrl = "https://zsyx-image.baidu.com/0/pic/197968965_762413223_1723451076.jpg";
        String uploadUrl = "https://www.feishu.cn/approval/openapi/v2/file/upload";
        String boundary = Long.toString(System.currentTimeMillis());

        try {

            // 获取图片数据
            URL url = new URL(imageUrl);
            HttpURLConnection imageConn = (HttpURLConnection) url.openConnection();
            imageConn.setRequestMethod("GET");
            InputStream imageInputStream = imageConn.getInputStream();

            // 上传图片
            HttpURLConnection uploadConn = (HttpURLConnection) new URL(uploadUrl).openConnection();
            uploadConn.setDoOutput(true);
            uploadConn.setRequestMethod("POST");
            uploadConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
            uploadConn.setRequestProperty("Authorization", "Bearer t-g1049uas6ZUUU6JW7CO3742GG5CXCNMZZ6EOT2RU");

            // 开启参数上传流
            DataOutputStream dos = new DataOutputStream(uploadConn.getOutputStream());
            dos.writeBytes("--" + boundary + "\r\n");
            dos.writeBytes("Content-Disposition: form-data; name=\"content\"; filename=\"ssb\";\r\n");
            dos.writeBytes("Content-Type: application/octet-stream\r\n\r\n");

            // 读取图片数据并写入上传流
            //            var buffer = new_byte_array(4096);
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = imageInputStream.read(buffer)) != -1) {
                dos.write(buffer, 0, bytesRead);
            }

            dos.writeBytes("\r\n");
            // Add the type field
            dos.writeBytes("--" + boundary + "\r\n");
            dos.writeBytes("Content-Disposition: form-data; name=\"type\";\r\n");
            dos.writeBytes("Content-Type: text/plain\r\n\r\n");
            dos.writeBytes("attachment\r\n");
            dos.writeBytes("--" + boundary + "\r\n");

            dos.writeBytes("Content-Disposition: form-data; name=\"name\";\r\n");
            dos.writeBytes("Content-Type: text/plain\r\n\r\n");
            dos.writeBytes("197968965_762413223_1723451076.jpg\r\n");
            dos.writeBytes("--" + boundary + "--\r\n");

            dos.flush();

            // 检查上传响应
            int responseCode = uploadConn.getResponseCode();
            String responseMessage = uploadConn.getResponseMessage();
            System.out.println("Response Code: " + responseCode);
            System.out.println("Response Message: " + responseMessage);


            BufferedReader in = new BufferedReader(new InputStreamReader(uploadConn.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 打印响应体
            System.out.println(response.toString());

            uploadConn.disconnect();

        } catch (IOException e){
            e.printStackTrace();
        }
    }
}
